package cn.wildfire.chat.app.extendsion.ui;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.List;

/**
 * Create by HEKE, 2019/8/21 9:32
 * TODO 手机在锁屏状态下点亮及开启通知管理对象
 */
public class LightThenNotifyPhone {

    private Context mContext;

    private WindowManager window;
    private WindowManager.LayoutParams layout;
    private LinearLayout view;

    private boolean isShowing;

    private void init() {
        window = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        layout = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layout.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layout.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layout.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        layout.format = PixelFormat.TRANSLUCENT;
        layout.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        layout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        layout.width = 1;
        layout.height = 1;
        layout.gravity = Gravity.LEFT | Gravity.TOP;

        view = new LinearLayout(mContext.getApplicationContext());
        view.setBackgroundColor(Color.WHITE);
    }

    public LightThenNotifyPhone(Context context) {
        this.mContext = context;
        init();
    }

    /**
     * 显示悬浮窗口
     */
    public void showWindow() {
        try {
            if (!isShowing) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (Settings.canDrawOverlays(mContext)) {
                        isShowing = true;
                        window.addView(view, layout);
                    }
                } else {
                    isShowing = true;
                    window.addView(view, layout);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏悬浮窗口
     */
    public void hideWindow() {
        try {
            if (isShowing) {
                isShowing = false;
                window.removeView(view);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * modify by HEKE , 2019/8/21 11:45
     * TODO 悬浮窗权限检测
     */
    private boolean checkOverlayPermission() {
        boolean ret = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(mContext)) {
                Toast.makeText(mContext, "需要悬浮窗权限", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + mContext.getPackageName()));
                List<ResolveInfo> infos = mContext.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
                ret = (infos == null || infos.isEmpty());
                if (!ret) {
                    mContext.startActivity(intent);
                }
            }
        }
        return ret;
    }

}
